{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Object-Oriented Programming\n", "\n", "Review of terms:\n", "\n", "* class\n", "* instance\n", "* method\n", "* global variables\n", "* types/classes with lowercase first letters are primitives (int, float, double, boolean)\n", "* constructor (none needed if no paramaters)\n", "* this\n", "* parameters, arguments" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_3\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_3\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_3\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_3\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #3:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #3 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "I'm a human\n", "I'm a human\n", "I'm a monkey\n" ] } ], "source": [ "class Human {\n", " String name;\n", " \n", " Human(String x) {\n", " name = x;\n", " }\n", " \n", " void speak() {\n", " println(\"I'm a human\");\n", " }\n", "}\n", "\n", "class Monkey {\n", " void speak() {\n", " println(\"I'm a monkey\");\n", " }\n", "}\n", "\n", "Human human0;\n", "Human human1;\n", "Monkey monkey0;\n", "\n", "void setup() {\n", " human0 = new Human(\"Kevin\");\n", " human1 = new Human(\"Sylvia\");\n", " monkey0 = new Monkey();\n", "}\n", "\n", "void draw() {\n", " human0.speak();\n", " human1.speak();\n", " monkey0.speak();\n", " noLoop();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arrays of Objects\n", "\n", "Review of ideas:\n", "\n", "* array\n", "* declare array\n", "* create space for the array\n", "* create the instances" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_8\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_8\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_8\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_8\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #8:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #8 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a human\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n", "I'm a monkey\n" ] } ], "source": [ "class Human {\n", " void speak() {\n", " println(\"I'm a human\");\n", " }\n", "}\n", "\n", "class Monkey {\n", " void speak() {\n", " println(\"I'm a monkey\");\n", " }\n", "}\n", "\n", "Human[] human = new Human[25];\n", "Monkey[] monkey = new Monkey[25];\n", "\n", "void setup() {\n", " for (int i=0; i < human.length; i++) {\n", " human[i] = new Human();\n", " }\n", " for (int i=0; i < monkey.length; i++) {\n", " monkey[i] = new Monkey();\n", " }\n", "}\n", "\n", "void draw() {\n", " for (int i=0; i < human.length; i++) {\n", " human[i].speak();\n", " }\n", " for (int i=0; i < monkey.length; i++) {\n", " monkey[i].speak();\n", " }\n", " noLoop();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Object-Oriented Programming - Inheritance " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_4\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_4\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_4\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_4\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #4:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #4 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "I'm a primate\n", "I'm a human\n", "I'm a monkey\n" ] } ], "source": [ "class Primate {\n", " void speak() {\n", " println(\"I'm a primate\");\n", " }\n", "}\n", "\n", "class Human extends Primate {\n", " void speak() {\n", " println(\"I'm a human\");\n", " }\n", "}\n", "\n", "class Monkey extends Primate {\n", " void speak() {\n", " println(\"I'm a monkey\");\n", " }\n", "}\n", "\n", "Primate primate0;\n", "Human human0;\n", "Monkey monkey0;\n", "\n", "void setup() {\n", " primate0 = new Primate();\n", " human0 = new Human();\n", " monkey0 = new Monkey();\n", "}\n", "\n", "void draw() {\n", " primate0.speak();\n", " human0.speak();\n", " monkey0.speak();\n", " noLoop();\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_4\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_4\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_4\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_4\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #4:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #4 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n", "I'm a monkey\n", "I'm a human\n" ] } ], "source": [ "class Primate {\n", " void speak() {\n", " println(\"I'm a primate\");\n", " }\n", "}\n", "\n", "class Human extends Primate {\n", " void speak() {\n", " println(\"I'm a human\");\n", " }\n", "}\n", "\n", "class Monkey extends Primate {\n", " void speak() {\n", " println(\"I'm a monkey\");\n", " }\n", "}\n", "\n", "class SpiderMonkey extends Monkey {\n", " void speak() {\n", " println(\"eeee eee ooo oo\");\n", " }\n", "}\n", "\n", "Primate[] primate = new Primate[25];\n", "\n", "void setup() {\n", " for (int i=0; i < primate.length; i++) {\n", " if (i % 2 == 0) {\n", " primate[i] = new Human();\n", " } else {\n", " primate[i] = new Monkey();\n", " }\n", " }\n", "}\n", "\n", "void draw() {\n", " for (int i=0; i < primate.length; i++) {\n", " primate[i].speak();\n", " }\n", " noLoop();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "New ideas:\n", "\n", "* extend a class\n", "* subclass\n", "* super class, or base class\n", "* method overrides\n", "* polymorphism\n", "\n", "1. Primate is the base class, or super class\n", "2. Human and Monkey are subclasses\n", "3. Treating all of the objects the same, but having the methods do their specifics: polymorphism\n", "\n", "\n", "See pages 223 - 232 of your textbook for additional information." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "java", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 2 }